package com.wu.util;

import cn.hutool.core.collection.CollectionUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 根据数字，进行按位运算
 */
public class BitOperatorUtils {
    /**
     *
     * @param userActive 用户活跃天数按位的整数
     * @param lastActive 用户最后一次活跃的时间戳（s）
     * @param lastCount 最近n天内
     * @return
     */
    public static Long getUserActiveDays(Long userActive, Long lastActive, Integer lastCount) {
        long nums = 0L;
        if (ObjectUtils.isNotEmpty(userActive) && ObjectUtils.isNotEmpty(lastActive) && userActive > 0 && lastActive > 0){
            long curTime = DateUtil.getCurrentDateStartTime().getTime() / 1000;
            long timeDiff = DateUtil.getDateDiff(curTime, lastActive);
            String binary = Long.toString(userActive, 2);
            if (timeDiff > 0){
                binary = plugZero(binary, timeDiff);
            }
            if (binary.length() > lastCount) {
                binary = binary.substring(binary.length() - lastCount, binary.length());
            }
            nums = getOneCount(binary);
        }
        return nums;
    }

    /**
     * 计算用户近n天活跃天数（aviator提供）
     * @param userActive 用户活跃天数按位的整数
     * @param lastActive 用户最后一次活跃的时间戳（s）
     * @param lastCount 最近n天内
     * @return
     */
    public static Long getActiveDaysForAviator(Long userActive, Long lastActive, Integer lastCount){
        long nums = 0L;
        if (ObjectUtils.isNotEmpty(userActive) && ObjectUtils.isNotEmpty(lastActive) && userActive > 0 && lastActive > 0){
            long curTime = DateUtil.getCurrentDateStartTime().getTime() / 1000;
            long timeDiff = DateUtil.getDateDiffForAviator(curTime, lastActive);
            String binary = Long.toString(userActive, 2);
            if (timeDiff > 0){
                binary = plugZero(binary, timeDiff);
            }
            if (binary.length() > lastCount) {
                binary = binary.substring(binary.length() - lastCount, binary.length());
            }
            nums = getOneCount(binary);
        }
        return nums;
    }

    /**
     * 给字符串最后补0
     * @param stringBinary
     * @param timeDiff
     * @return
     */
    public static String plugZero(String stringBinary, Long timeDiff) {
        String res = stringBinary;
        if (StringUtils.isNotEmpty(stringBinary)){
            StringBuilder sb = new StringBuilder(stringBinary);
            for (int i = 0; i < timeDiff; i++) {
                sb.append('0');
            }
            res = sb.toString();
        }

        return res;
    }

    /**
     * 获取字符串中有多少个1
     * @param stringBinary
     * @return
     */
    public static Long getOneCount(String stringBinary) {
        long count = 0L;
        if(StringUtils.isNotEmpty(stringBinary)){
            for (int i = 0; i < stringBinary.length(); i++) {
                if (stringBinary.charAt(i) == '1') {
                    count++;
                }
            }
        }
        return count;
    }

    /**
     * 获取用户累计播放次数(aviator提供)
     * 
     * @param playRecord 播放记录
     * @param lastPlay 最后一次播放时间戳（s）
     * @param lastCount 最近n天内
     * @return
     */
    public static Long getPlayCountsForAviator(String playRecord, Long lastPlay, Integer lastCount) {
        long result = 0L;
        if (StringUtils.isNotBlank(playRecord) && ObjectUtils.isNotEmpty(lastPlay) && lastCount > 0 && lastPlay > 0) {
            long curTime = DateUtil.getCurrentDateStartTime().getTime() / 1000;
            long timeDiff = DateUtil.getDateDiffForAviator(curTime, lastPlay);
            if (timeDiff > 0) {
                playRecord = FillZero(playRecord, timeDiff, lastCount);
            }
            List<String> playCountList = List.of(playRecord.split(","));
            if (CollectionUtil.isEmpty(playCountList)) {
                return result;
            }
            if (playCountList.size() > lastCount) {
                playCountList = playCountList.subList(playCountList.size() - lastCount, playCountList.size());
            }
            result = playCountList.stream().mapToInt(Integer::valueOf).sum();
        }
        return result;
    }

    /**
     * 播放次数末尾补充0
     * 
     * @param playCounts
     * @param timeDiff
     * @param lastCount
     * @return
     */
    public static String FillZero(String playCounts, Long timeDiff ,Integer lastCount) {
        String res = playCounts;

        if (StringUtils.isNotEmpty(playCounts)) {
            //补位的长度
            int fillLength = Math.toIntExact(timeDiff);
            //如果最后一次播放天数距今大于"最近n天"，则直接补"最近n天"长度的0字符串
            if (timeDiff > lastCount) {
                fillLength= lastCount;
            }
            StringBuilder sb = new StringBuilder(playCounts);
            for (int i = 0; i < fillLength; i++) {
                sb.append(",0");
            }
            res = sb.toString();
        }

        return res;
    }

}
